//
//  ESWeatherNetResult.m
//  EasyStyle
//
//  Created by FK on 16/1/27.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESWeatherNetResult.h"

@implementation ESWeatherNetResult

//- (id)init
//{
//    self = [super init];
//    if (self) {
//    }
//    return self;
//}

// 解析业务数据
- (void)parseNetResult:(NSDictionary *)jsonDictionary
{
    [super parseNetResult:jsonDictionary];
    
    if ([self.errorCode intValue] != 0) {
        return;
    }
    
    _resultDict = [self parseWeatherDataWithResultData:jsonDictionary];
}

- (NSDictionary *)parseWeatherDataWithResultData:(NSDictionary *)jsonResult
{
    NSDictionary *jsonData = jsonResult[@"result"][@"data"];
    
    /// 实时天气
    NSDictionary *realtimeDict = jsonData[@"realtime"];
    
    NSDictionary *wind = realtimeDict[@"wind"];
    NSDictionary *realtimewWeather = realtimeDict[@"weather"];
    
    NSDictionary *realtime = @{@"windspeed":wind[@"windspeed"],
                               @"direct":wind[@"direct"],
                               @"power":wind[@"power"],
                               @"humidity":realtimewWeather[@"humidity"],
                               @"img":realtimewWeather[@"img"],
                               @"info":realtimewWeather[@"info"],
                               @"temperature":realtimewWeather[@"temperature"],
                               @"moon":realtimeDict[@"moon"],
                               @"date":realtimeDict[@"date"],
                               @"week":realtimeDict[@"week"],
                               @"cityName":realtimeDict[@"city_name"]};
    
    
    /// 生活tips
    NSDictionary *life = @{};
    
    
    /// 近期天气状况
    NSArray *weatherArray = jsonData[@"weather"];
    
    NSMutableArray *oneWeekWeather = [NSMutableArray arrayWithCapacity:weatherArray.count];
    for (int inx = 0; inx < weatherArray.count; inx++) {
        NSDictionary *dict = weatherArray[inx];
        
        NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithCapacity:2];
        
        NSString *dayORnight;
        for (int i = 0; i < 2; i++) {
            (i == 0)?(dayORnight = @"night"):(dayORnight = @"day");
            
            NSArray *dayORnightArray = dict[@"info"][dayORnight];
            
            NSDictionary *curInfo = @{@"date":dict[@"date"],
                                      @"week":dict[@"week"],
                                      @"nongli":dict[@"nongli"],
                                      @"weatherImgID":dayORnightArray[0],
                                      @"weatherDesc":dayORnightArray[1],
                                      @"weathertemperature":dayORnightArray[2],
                                      @"weatherWindDirect":dayORnightArray[3],
                                      @"weatherWindPower":dayORnightArray[4]};
            
            [infoDict setValue:curInfo forKey:dayORnight];
        }
        [oneWeekWeather addObject:infoDict];
    }
    
    
    /// PM2.5
    NSDictionary *PM25Dict = jsonData[@"pm25"][@"pm25"];
    
    NSDictionary *PM25 = @{@"curPm":PM25Dict[@"curPm"],
                           @"pm25":PM25Dict[@"pm25"],
                           @"pm10":PM25Dict[@"pm10"],
                           @"level":PM25Dict[@"level"],
                           @"quality":PM25Dict[@"quality"],
                           @"des":PM25Dict[@"des"]};
    
    NSDictionary *outDict = @{ESWeatherNetResult_realtime:realtime,
                              ESWeatherNetResult_weatherLife:life,
                              ESWeatherNetResult_oneWeekWeather:oneWeekWeather,
                              ESWeatherNetResult_PM25:PM25};
    return outDict;
}

- (void)getDetailWeatherData:(NSDictionary *)detailData andDataType:(NSString *)dataType
{
    if ([dataType isEqualToString:ESWeatherNetResult_realtime]) {
        
        [self setRealtimeWithDict:detailData[dataType]];
        
    }else if ([dataType isEqualToString:ESWeatherNetResult_oneWeekWeather]) {
        
        [self setOneWeekWeatherWithArray:detailData[dataType]];
        
    }else if ([dataType isEqualToString:ESWeatherNetResult_weatherLife]) {
        
        [self setWeatherLifeWithDict:detailData[dataType]];
        
    }else if ([dataType isEqualToString:ESWeatherNetResult_PM25]) {
        
        [self setPM25WithDict:detailData[dataType]];
        
    }else {
        [self setRealtimeWithDict:detailData[ESWeatherNetResult_realtime]];
        [self setOneWeekWeatherWithArray:detailData[ESWeatherNetResult_oneWeekWeather]];
        [self setWeatherLifeWithDict:detailData[ESWeatherNetResult_weatherLife]];
        [self setPM25WithDict:detailData[ESWeatherNetResult_PM25]];
    }
}

- (void)setRealtimeWithDict:(NSDictionary *)detailData
{
    
    _realtime = [WeatherRealtime new];
    
    if (detailData != nil) {
        _realtime.windspeed = detailData[@"windspeed"];
        _realtime.direct = detailData[@"direct"];
        _realtime.power = detailData[@"power"];
        _realtime.humidity = detailData[@"humidity"];
        _realtime.img = detailData[@"img"];
        _realtime.info = detailData[@"info"];
        _realtime.temperature = detailData[@"temperature"];
        _realtime.moon = detailData[@"moon"];
        _realtime.date = detailData[@"date"];
        _realtime.week = detailData[@"week"];
        _realtime.cityName = detailData[@"cityName"];
    }
}

- (void)setOneWeekWeatherWithArray:(NSArray *)detailData
{
    _oneWeekWeather = [NSMutableArray arrayWithCapacity:[detailData count]];
    
    if (detailData != nil) {
        for (NSDictionary *dict in detailData) {
            
            NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithCapacity:2];
            NSString *dayORnight;
            for (int i = 0; i < 2; i++) {
                (i == 0)?(dayORnight = @"night"):(dayORnight = @"day");
                
                NSDictionary *dayORnightDict = dict[dayORnight];
                
                WeatherOneWeekInfo *item = [WeatherOneWeekInfo new];
                
                item.weatherImgID = dayORnightDict[@"weatherImgID"];
                item.weatherDesc = dayORnightDict[@"weatherDesc"];
                item.weathertemperature = dayORnightDict[@"weathertemperature"];
                item.weatherWindDirect = dayORnightDict[@"weatherWindDirect"];
                item.weatherWindPower = dayORnightDict[@"weatherWindPower"];
                item.weatherDate = dayORnightDict[@"date"];
                item.weatherWeek = dayORnightDict[@"week"];
                item.weatherNongli = dayORnightDict[@"nongli"];

                [infoDict setValue:item forKey:dayORnight];
            }
            [_oneWeekWeather addObject:infoDict];
        }
    }
}

- (void)setWeatherLifeWithDict:(NSDictionary *)detailData
{
    _weatherLife = [WeatherLife new];
    
    if (detailData != nil) {
        
    }
}

- (void)setPM25WithDict:(NSDictionary *)detailData
{
    _PM25 = [WeatherPM25 new];
    
    if (detailData != nil) {
        _PM25.curPm = detailData[@"curPm"];
        _PM25.pm25 = detailData[@"pm25"];
        _PM25.pm10 = detailData[@"pm10"];
        _PM25.level = detailData[@"level"];
        _PM25.quality = detailData[@"quality"];
        _PM25.des = detailData[@"des"];
        
    }
}

@end

@implementation WeatherRealtime

@end

@implementation WeatherLife

@end

@implementation WeatherOneWeekInfo

@end

@implementation WeatherPM25

@end